package com.monitor.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import net.sf.json.JSONObject;

public class TestPost{
	public String testPost(String carPlaces) throws IOException{
		System.out.println(new Date().getTime()+"++++++++++++++++++++++++");
		String urls = "http://position.v-infonet.com/api/Mileage";
		URL url = new URL(urls);
		URLConnection connection = url.openConnection();
		connection.setDoOutput(true);
		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
		out.write("licensePlate=" + carPlaces); //post的关键所在！
		//out.write("datetime=" + "2015-10-02");
		out.flush();
		out.close();
		String sCurrentLine;
		String sTotalString;
		sCurrentLine = "";
		sTotalString = "";
		InputStream l_urlStream;
		l_urlStream = connection.getInputStream();
		BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream, "UTF-8"));
		while(( sCurrentLine = l_reader.readLine() ) != null){
			sTotalString += sCurrentLine + "\r\n";
		}
		JSONObject jsStr = JSONObject.fromObject(sTotalString);
		System.out.println(new Date().getTime()+"----------------------------");
		System.out.println(jsStr.toString());
		return jsStr.get("Data").toString();
	}
}
